<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/6
 * Time: 15:34
 */

namespace Xcx\Model;


class AddressModel extends BaseModel
{

    /**
     *  新增用户地址
     * @param $name
     * @param $phone
     * @param $province
     * @param $city
     * @param $area
     * @param $address
     * @param $isDefault
     * @return mixed
     */
    public function addAddress($name, $phone, $province, $city, $area, $address, $isDefault)
    {

        $data['uid'] = $this->uid;
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['province'] = $province;
        $data['city'] = $city;
        $data['area'] = $area;
        $data['address'] = $address;
        $data['default'] = $isDefault;
        $data['add_time'] = time();

        if ($isDefault == '1') {
            M()->startTrans();
            $res1 = $this->cancelDefaultAddress();
            $res2 = $this->data($data)->add();

            if ($res1 && $res2) {
                M()->commit();
                return true;
            } else {
                M()->rollback();
                return false;
            }
        } else {
            return $this->data($data)->add();
        }
    }

    /**
     *  更新用户地址
     * @param $id
     * @param $name
     * @param $phone
     * @param $province
     * @param $city
     * @param $area
     * @param $address
     * @param $isDefault
     * @return mixed
     */
    public function updateAddress($id, $name, $phone, $province, $city, $area, $address, $isDefault)
    {
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['province'] = $province;
        $data['city'] = $city;
        $data['area'] = $area;
        $data['address'] = $address;
        $data['default'] = $isDefault;

        $maps['id'] = $id;
        $maps['uid'] = $this->uid;
        if ($isDefault == '1') {
            M()->startTrans();
            $res1 = $this->cancelDefaultAddress();
            $res2 = $this->data($data)->where($maps)->save();

            if ($res1 && $res2) {
                M()->commit();
                return true;
            } else {
                M()->rollback();
                return false;
            }
        } else {
            return $this->data($data)->where($maps)->save();
        }
    }

    /**
     * 取消用户其他默认地址
     * @param $id
     * @return bool|false|int
     */
    public function cancelDefaultAddress($id = 0)
    {
        $id == 0 ? '' : $maps['id'] = array('NEQ',$id);
        $maps['uid'] = $this->uid;
        $res = $this->where($maps)->save(array('default'=>'0'));
        if ($res || $res == 0) return true;
        return false;
    }

    /**
     * 获取地址列表
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAddressList()
    {
        return $this->where(array('uid'=>$this->uid))->order(array('default'=>'desc'))->select();
    }

    /**
     * 删除地址
     * @param $id
     * @return int|mixed
     */
    public function deleteAddress($id)
    {
        return $this->where(array('uid'=>$this->uid,'id'=>$id))->delete();
    }

    /**
     * 获取用户默认地址
     * @return array
     */
    public function getDefaultAddress()
    {
        $address = $this->where(array('uid'=>$this->uid))->order(array('default'=>'desc','add_time'=>'desc'))->find();
        if (is_array($address) && count($address) > 0) return $address;
        return false;
    }

    /**
     * 获取地址详情
     * @param $id
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getAddressDetailsInfo($id)
    {
        return $this->where(array('uid'=>$this->uid,'id'=>$id))->find();
    }
}